package com.service.Impl;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.CUsedMapper;
import com.mapper.UserInfoMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CUsed;
import com.pojo.entity.SysUserInfo;
import com.pojo.vo.CUsedVo;
import com.pojo.vo.SelectVo;
import com.service.CUsedService;
import com.service.SysDictionaryService;
import com.service.SysUserInfoService;
import com.service.SysUtilService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("CUsedService")
public class CUsedServiceImpl implements CUsedService {
    @Autowired
    Snowflake snowflake;
    @Autowired
    CUsedMapper cUsedMapper;
    @Autowired
    SysUtilService sysUtilService;
    @Autowired
    SysDictionaryService sysDictionaryService;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SysUserInfoService sysUserInfoService;
    @Override
    @Cacheable(cacheNames = "service-cache",key="#root.methodName")
    public Message<List<CUsedVo>> list(CUsedVo cusedvo) {
        QueryWrapper<CUsed> wrapper=new QueryWrapper<>();
        if (cusedvo != null) {
            if (cusedvo.getUserType() != null) wrapper.eq("user_type", cusedvo.getUserType());
            if (cusedvo.getUserNum() != null) wrapper.eq("user_num", cusedvo.getUserNum());
            if (cusedvo.getCityNum() != null) wrapper.eq("city_num", cusedvo.getCityNum());
            if (cusedvo.getCargo() != null) wrapper.like("cargo", cusedvo.getCargo());
            if (cusedvo.getState() != null) wrapper.eq("state", cusedvo.getState());
            if (cusedvo.getChangeType() != null) wrapper.eq("change_type", cusedvo.getChangeType());
        }
        List<CUsed> cUseds = cUsedMapper.selectList(wrapper);
        List<CUsedVo> message = cUseds.stream().map(c -> {
                    CUsedVo cUsedVo = PojoUtil.copyObject(c, CUsedVo.class);
            SelectVo city_num = sysUtilService.selectone(cUsedVo.getCityNum(), "syscity");
            cUsedVo.setCityName(city_num.getText());
            SelectVo c_cused = sysUtilService.selectone(cUsedVo.getUserType(), "c_cused");
            cUsedVo.setUserTypeName(c_cused.getText());
            SelectVo dam_type = sysUtilService.selectone(cUsedVo.getUserType(), "dam_type");
            cUsedVo.setDamName(dam_type.getText());
            String str= String.valueOf(cUsedVo.getChangeType());
            List list=new ArrayList();
            for (int i = 0; i <str.length(); i++) {
                String subStr = str.substring(i, i+1);
                SelectVo change_type = sysUtilService.selectone(Long.valueOf(subStr), "change_type");
                list.add(change_type.getText());
            }
            cUsedVo.setTypeName(list);
            SysUserInfo select = sysUserInfoService.select(cUsedVo.getUserNum());
            cUsedVo.setUserName(select.getRealName());
            return cUsedVo;
                }
        ).collect(Collectors.toList());
        return Message.successMessage((long) cUseds.size(), message);
    }
    /**
     * 通过ID查询单条信息
     * @param cUsedVo
     * @return
     */
    @Override
    public Message<CUsedVo> get(CUsedVo cUsedVo) {
        if (cUsedVo != null) {
            QueryWrapper<CUsed> wrapper = new QueryWrapper<>();
            wrapper.eq("id", cUsedVo.getId());
            CUsed cUsed = cUsedMapper.selectOne(wrapper);
            return Message.successMessage(1L, PojoUtil.copyObject(cUsed, CUsedVo.class));
        }
        return  Message.errorMessage("1001","查询失败");
    }
    /**
     * 修改数据信息
     * @param cUsedVo
     * @return
     */
    @Override
    public Message<CUsedVo> edit(CUsedVo cUsedVo) {
        CUsed cUsed = PojoUtil.copyObject(cUsedVo, CUsed.class);
        int i = cUsedMapper.updateById(cUsed);//根据ID更新book表
        if (i==0)return Message.errorMessage("1001","更新失败");
        else return Message.successMessage(1L,cUsedVo);
    }
    /**
     * 添加数据信息
     * @param cUsedVo
     * @return
     */
    @Override
    public Message<CUsedVo> add(CUsedVo cUsedVo) {
        CUsed cUsed = PojoUtil.copyObject(cUsedVo, CUsed.class);
        int i= cUsedMapper.insert(cUsed);
        if (i==0)return Message.errorMessage("1001","添加失败");
        else return Message.successMessage(1L,cUsedVo);
    }
    /**
     * 删除信息
     * @param cUsedVo
     * @return
     */
    @Override
    public Message<CUsedVo> delete(CUsedVo cUsedVo) {
        int i = cUsedMapper.deleteById(cUsedVo.getId());
        if (i==0)return Message.errorMessage("1001","删除失败");
        else return Message.successMessage(1L,null);
    }
}
